/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.martinkrasa.golftourweb.site;

import com.martinkrasa.golftourweb.site.LoadingService.LoadingException;
import com.martinkrasa.golftourweb.site.entity.TournamentResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

/**
 *
 * @author martin.krasa
 */
@Service
public class HTMLParserLoadingService implements LoadingService {

    /**
     *
     * @param bruttoUrlStrings
     * @param nettoUrlStrings
     * @return 
     * @throws IOException
     * @throws com.martinkrasa.golftourweb.site.LoadingService.LoadingException
     */
    @Override
    public List<TournamentResult> loadTournament(String[] bruttoUrlStrings, String[] nettoUrlStrings) throws IOException, LoadingException {

        List<TournamentResult> tournamentResultList = new ArrayList<>();
        
        Set<Document> docsBrutto = new HashSet<>();
        for (String bruttoUrlString : bruttoUrlStrings) {
            docsBrutto.add(Jsoup.connect("http://cgf.cz/TournResultCategory.aspx?IDTournCategory=" + bruttoUrlString).timeout(0).get());
        }

        String selectorRecordTable = "table#ctl00_MainPlaceHolder_ResultList1_gvResultList";
        String selectorRows = "tr[class*=Row]";

        Document docNetto;
        for (String nettoUrlString : nettoUrlStrings) {

            docNetto = Jsoup.connect("http://cgf.cz/TournResultCategory.aspx?IDTournCategory=" + nettoUrlString).timeout(0).get();

            Element recordTableNetto = docNetto.select(selectorRecordTable).first();
            if (recordTableNetto != null) {

                Elements rows = recordTableNetto.select(selectorRows);
                for (Element row : rows) {

                    if (row.childNodeSize() > 6) {

                        // search netto data
                        Elements line = row.select("td");

                        int rank = Integer.parseInt(line.get(0).text());

                        String playerName = line.get(1).text();

                        String clubAbbreviation = line.get(2).text();

                        String cardId = line.get(3).text();

                        int netto = Integer.parseInt(line.get(6).text());

                        int brutto = 0;

                        // search brutto data
                        for (Document docBrutto : docsBrutto) {

                            Element recordTableBrutto = docBrutto.select(selectorRecordTable).first();
                            if (recordTableBrutto != null) {

                                Elements rowsBrutto = recordTableBrutto.select(selectorRows);
                                for (Element rowBrutto : rowsBrutto) {
                                    Elements lineBrutto = rowBrutto.select("td");

                                    String cardIdBrutto = lineBrutto.get(3).text();

                                    if (cardIdBrutto.equals(cardId)) {
                                        brutto = Integer.parseInt(lineBrutto.get(6).text());
                                    }
                                }
                            } else {
                                throw new LoadingException("No brutto table found.");
                            }
                        }

                        // add row to the list
                        TournamentResult tournamentResult = new TournamentResult();
                        tournamentResult.setId(0);
                        tournamentResult.setTournamentId(0);
                        tournamentResult.setRank(rank); 
                        tournamentResult.setPlayerName(playerName); 
                        tournamentResult.setClubAbbreviation(clubAbbreviation);
                        tournamentResult.setCardId(cardId);
                        tournamentResult.setBruttoResult(brutto);
                        tournamentResult.setNettoResult(netto);
                        tournamentResult.setResult(0);
                        
                        tournamentResultList.add(tournamentResult);
                    } else {
                        throw new LoadingException("Wrong formate of the table.");
                    }
                }
            } else {
                throw new LoadingException("No netto table found.");
            }
        }
        
        return tournamentResultList;
    }

}
